﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebSite.Core
{
    /// <summary>
    ///WebBasePage 的摘要说明
    /// </summary>
    public class BasePage : Page
    {
        /// <summary>
        /// 是否需要登录
        /// </summary>
        public virtual bool NeedLogin
        {
            get { return true; }
        }

        protected override void OnLoad(EventArgs e)
        {
            PageInit();
            base.OnLoad(e);
        }
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
        }

        /// <summary>
        /// 是否登录
        /// </summary>
        public bool IsLogin
        {
            get
            {
                bool retrunBool = false;
                if (Project_Session.CurrentUser != null)
                {
                    if (!string.IsNullOrEmpty(Project_Session.CurrentUser.UserName))
                    {
                        retrunBool = true;
                    }
                }
                return retrunBool;

            }
        }

        /// <summary>
        /// 当前登录用户
        /// </summary>
        public LoginUser CurrentUser
        {
            get
            {
                return Project_Session.CurrentUser;
            }
            set
            {
                Project_Session.CurrentUser = value;
            }
        }

        /// <summary>
        /// 页面初始化
        /// </summary>
        protected void PageInit()
        {
            //注销
            if (Request.QueryString["login"] == "loginout")
            {
                LoginOut();
            }

            if (NeedLogin && !IsLogin)
            {
                // AlertMessage("登录过期，请先登录", ConstHelper.DefaultUrl);
                AlertMessage( "登录过期，请先登录", "/Reg.aspx" );
            }

        }


        /// <summary>
        /// 注销
        /// </summary>
        public void LoginOut()
        {
            CurrentUser = null;
            Project_Session.ClearSession();
            Response.Redirect(ConstHelper.DefaultUrl);
        }


        #region 跳转提示页面

        /// <summary>
        /// 提示信息，页面Back
        /// </summary>
        /// <param name="message"></param>
        public void AlertMessageBack(string message)
        {
            this.AlertMessage(message, string.Empty, true);
        }
        /// <summary>
        /// 提示信息，页面不back
        /// </summary>
        /// <param name="message"></param>
        public void AlertMessage(string message)
        {
            this.AlertMessage(message, string.Empty, false);
        }
        /// <summary>
        /// 提示信息，页面重定向
        /// </summary>
        /// <param name="message"></param>
        /// <param name="url"></param>
        public void AlertMessage(string message, string url)
        {
            this.AlertMessage(message, url, false);
        }
        /// <summary>
        /// 提示信息，可以重定向，可以back
        /// </summary>
        /// <param name="message"></param>
        /// <param name="Url"></param>
        /// <param name="back"></param>
        public void AlertMessage(string message, string Url, bool back)
        {
            string alert = string.Format(@"<script>alert('" + message.Replace("'", "\'") + "');");
            if (back)
            {
                alert += "history.back();";
            }
            else if (!string.IsNullOrEmpty(Url))
            {
                alert += "window.location.href='" + Url + "';";
            }
            alert += "</script>";

            HttpContext.Current.Response.Write(alert);
            HttpContext.Current.Response.End();
        }


        public void AlertMessage(string message, string Url, bool back, bool IsTop)
        {
            string pageLayer = string.Empty;
            if (IsTop)
            {
                pageLayer = "top";
            }
            else
            {
                pageLayer = "window";
            }

            string alert = string.Format(@"<script>alert('" + message.Replace("'", "\'") + "');");
            if (back)
            {
                alert += "history.back();";
            }
            else if (!string.IsNullOrEmpty(Url))
            {
                alert += pageLayer + ".location.href='" + Url + "';";
            }
            alert += "</script>";

            HttpContext.Current.Response.Write(alert);
            HttpContext.Current.Response.End();
        }

        public void ServerRedirect(string Url, bool back, bool IsTop)
        {
            string pageLayer = string.Empty;
            if (IsTop)
            {
                pageLayer = "top";
            }
            else
            {
                pageLayer = "window";
            }

            string alert = string.Format(@"<script>");
            if (back)
            {
                alert += "history.back();";
            }
            else if (!string.IsNullOrEmpty(Url))
            {
                alert += pageLayer + ".location.href='" + Url + "';";
            }
            alert += "</script>";

            HttpContext.Current.Response.Write(alert);
            HttpContext.Current.Response.End();
        }

        public void SetSelectedIndexByText( System.Web.UI.HtmlControls.HtmlSelect ctrl, string text )
        {
            foreach (ListItem i in ctrl.Items)
            {
                if (i.Text == text)
                {
                    ctrl.SelectedIndex = -1;
                    i.Selected = true;
                    break;
                }
            }
        }

        public void SetSelectedIndexByValue( System.Web.UI.HtmlControls.HtmlSelect ctrl, string value )
        {
            foreach (ListItem i in ctrl.Items)
            {
                if (i.Value == value)
                {
                    ctrl.SelectedIndex = -1;
                    i.Selected = true;
                    break;
                }
            }
        }

        //public static void ShowMessage(string message)
        //{
        //    ShowMessage(message, string.Empty, true, null);
        //}
        //public static void ShowMessage(string message, bool back, params string[] para)
        //{
        //    ShowMessage(message, string.Empty, back, para);
        //}
        /// <summary>
        /// 提示信息
        /// </summary>
        /// <param name="message">提示文字，可用占位符</param>
        /// <param name="Url"></param>
        /// <param name="back"></param>
        /// <param name="para">占位符</param>
        //        public void ShowMessage(string message, string Url, bool back, params string[] para)
        //        {

        //            Session["MessageNotify"] = para == null ? message : string.Format(message, para);// code;// MessageSetting.GetMessage(code, para);


        //            if (string.IsNullOrEmpty(Url))
        //            {
        //                Url = Request.QueryString["returnurl"];
        //            }

        //            if (!string.IsNullOrEmpty(Url))
        //            {
        //                Session["MessageNotify"] += @"
        //<script type=""text/javascript"">setTimeout(function(){window.location.href='" + Url + "';},3000);</script>";
        //            }
        //            else
        //            {
        //                if (back)
        //                {
        //                    Session["MessageNotify"] += @"<script type=""text/javascript"">setTimeout(function(){window.history.back();},3000);</script>";
        //                }
        //            }
        //            Response.Redirect("~/message.aspx", true);
        //        }
        #endregion


        #region---分页参数----

        private int pageSize = 5;
        /// <summary>
        /// 页码数
        /// </summary>
        protected virtual int PageSize
        {
            get
            {
                return pageSize;
            }

        }
        //分页参数
        public int PageIndex
        {
            get
            {

                int p = 1;
                int.TryParse(Request.QueryString["pindex"], out p);
                return Math.Max(1, p);
            }
        }

        #endregion


       

    }
}